<template>
  <div class="header-container">
    <div class="header-left flex-box">
      <el-icon class="icon" size="20" @click="store.commit('collapseMenu')">
        <Fold />
      </el-icon>
      <ul class="flex-box">
        <li
          v-for="(item, index) in selectMenu"
          :key="item.path"
          class="title-box"
          :class="{ active: route.path === item.path }"
        >
          <el-icon size="13">
            <component :is="item.icon"></component>
          </el-icon>
          <router-link :to="item.path">{{ item.name }}</router-link>
          <el-icon class="close" @click="closeTab(item, index)">
            <Close />
          </el-icon>
        </li>
      </ul>
    </div>
    <div class="header-right">
      <el-dropdown>
        <div class="el-dropdown-link flex-box">
          <el-avatar src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png" />
          <p class="user-admin">admin</p>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>Action 1</el-dropdown-item>
            <el-dropdown-item>Action 2</el-dropdown-item>
            <el-dropdown-item>Action 3</el-dropdown-item>
            <el-dropdown-item disabled>Action 4</el-dropdown-item>
            <el-dropdown-item divided>Action 5</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup>
import { useStore } from 'vuex'
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
const store = useStore()
const route = useRoute()
const router = useRouter()
const selectMenu = computed(() => store.state.menu.selectMenu)

// 删除被关闭的tag数据
const closeTab = (item, index) => {
  store.commit('closeMenu', item)
  if (route.path === item.path) {
    // 如果点击是最后一位 就往前进一位
    if (index === selectMenu.value.length) {
      if (selectMenu.value.length) {
        router.push(selectMenu.value[index - 1].path)
      } else {
        router.push('/')
      }
    } else {
      // 如果点击的不是最后一位
      router.push(selectMenu.value[index].path)
    }
  }
}
</script>

<style scoped lang="less">
.flex-box {
  display: flex;
  align-items: center;
  height: 100%;
}

.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  background: #fff;
  padding-right: 25px;

  .header-left {
    height: 100%;

    .icon {
      width: 45px;
      height: 100%;
    }

    .icon:hover {
      background: #f5f5f5;
      cursor: pointer;
    }
  }

  .header-right {
    .user-admin {
      margin-left: 10px;
    }
  }

  ul {
    height: 100%;

    .title-box {
      height: 100%;
      display: flex;
      align-items: center;
      font-size: 12px;

      a {
        padding: 0 3px 0px 6px;
        color: black;
      }

      .close {
        visibility: hidden;
      }

      &:hover {
        cursor: pointer;
        background: #f5f5f5;

        .close {
          visibility: visible;
        }
      }
    }

    .active {
      color: #409eff;

      a {
        color: #409eff;
      }
    }
  }
}
</style>
